package com.commerce.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.commerce.common.Reminder;
import com.commerce.common.WXConfig;
import com.commerce.common.WXJsonResult;
import com.commerce.constant.UserType;
import com.commerce.mapping.UserMapping;
import com.commerce.model.User;
import com.commerce.service.UserService;
import com.commerce.util.HTTPUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wyj
 * @Title:
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapping userMapping;

    @Autowired
    private WXConfig wxConfig;

    /**
     * 新增
     *
     * @param user
     * @return
     */
    @Override
    public String insert(User user, HttpServletRequest request) {
        return userMapping.insert(user)>0? Reminder.success.name():Reminder.saveerror.name();
    }

    /**
     * 修改
     *
     * @param user
     * @return
     */
    @Override
    public String update(User user, HttpServletRequest request) {
        return null;
    }

    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @Override
    public String deleteById(int id) {
        return null;
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @Override
    public User selectById(int id) {
        return null;
    }

    /**
     * 登录小程序，获取用户数据
     *
     * @param session
     * @param code
     * @return
     */
    @Override
    public WXJsonResult login(String code , HttpSession session) {
        Map<String,Object> usermap = new HashMap<>();
        User userdata =(User)session.getAttribute(UserType.USER.name());
        if(userdata != null){
            usermap.put(UserType.USER.name(), userdata);
            return WXJsonResult.returnWXJsonResult(Reminder.wxuserloginerror.name(),usermap);
        }
        String json = HTTPUtil.HTTPGet("https://api.weixin.qq.com/sns/jscode2session?appid="+wxConfig.getAPPID()+"&secret="+wxConfig.getAppSecret()+"&js_code="+code+"&grant_type=authorization_code");
        Map<String,String> map  = JSONObject.parseObject(json, Map.class);
        String openid = map.get("openid");
        userdata = userMapping.selectByOpenId(openid);
        if(userdata ==  null){
            userdata = new User();
            userdata.setOpenid(openid);
            userMapping.insert(userdata);
        }else{
            userMapping.updateByUseTime(userdata.getId());
        }
        if(userdata.getId() == 0 ){
            return WXJsonResult.returnWXJsonResult(Reminder.wxuserloginerror.getName(),null);
        }
        usermap.put(UserType.USER.name(), userdata);
        session.setAttribute(UserType.USER.name(), userdata);
        return WXJsonResult.returnWXJsonResult(Reminder.success.getName(),usermap);
    }
}
